package com.bey.bow.web.course;

import java.io.File;
import java.io.IOException;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.bey.bow.entity.Course;
import com.bey.bow.entity.User;
import com.bey.bow.service.course.CourseService;
import com.bey.bow.utils.FileNameGenerator;
import com.bey.bow.utils.UserUtils;

@Controller
@RequestMapping(value = "admin/course")
public class CourseAdminController {

	private static final int PAGE_SIZE = 10;

	private CourseService courseService;

	@RequestMapping(value = "create", method = RequestMethod.GET)
	public String createForm(Model model) {
		model.addAttribute("course", new Course());
		model.addAttribute("action", "create");
		return "admin/course/courseForm";
	}

	@RequestMapping(value = "create", method = RequestMethod.POST)
	public String create(Course course,
			@RequestParam("file") MultipartFile file,
			RedirectAttributes redirectAttributes, HttpServletRequest request) {
		if (file.isEmpty()) {
			return "/admin/course/course_add_fail";
		}

		String path = request.getSession().getServletContext()
				.getRealPath("static/upload/");
		String fileName = FileNameGenerator.getNewFileName(file);
		String filePath = path + "/" + fileName;
		course.setCourseImg(fileName);
		File dest = new File(filePath);

		try {
			file.transferTo(dest);
		} catch (IllegalStateException e) {
			e.printStackTrace();
			return "/admin/course/course_add_fail";
		} catch (IOException e) {
			e.printStackTrace();
			return "/admin/course/course_add_fail";
		}

		User user = new User(UserUtils.getCurrentUserId());
		course.setUser(user);

		courseService.saveCourse(course);
		redirectAttributes.addFlashAttribute("message", "创建任务成功");
		return "redirect:/admin/course/";
	}

	@RequestMapping(value = "update/{id}", method = RequestMethod.GET)
	public String updateForm(@PathVariable("id") Long id, Model model) {
		model.addAttribute("course", courseService.getCourse(id));
		model.addAttribute("action", "update");
		return "admin/course/courseForm";
	}

	@RequestMapping(value = "update", method = RequestMethod.POST)
	public String update(@ModelAttribute("course") Course course,
			RedirectAttributes redirectAttributes) {

		User user = new User(UserUtils.getCurrentUserId());
		course.setUser(user);

		courseService.saveCourse(course);
		redirectAttributes.addFlashAttribute("message", "更新任务成功");
		return "redirect:/admin/course/";
	}

	@RequestMapping(value = "delete/{id}")
	public String delete(@PathVariable("id") Long id,
			RedirectAttributes redirectAttributes) {
		courseService.deleteCourse(id);
		redirectAttributes.addFlashAttribute("message", "删除课程成功");
		return "redirect:/admin/course/";
	}

	@RequestMapping(value = "")
	public String list(
			@RequestParam(value = "sortType", defaultValue = "auto") String sortType,
			@RequestParam(value = "page", defaultValue = "1") int pageNumber,
			Model model, ServletRequest request) {

		Page<Course> courses = courseService.getCourses(pageNumber, PAGE_SIZE,
				"auto");

		model.addAttribute("courses", courses);

		return "admin/course/courseList";
	}

	@Autowired
	public void setCourseService(CourseService courseService) {
		this.courseService = courseService;
	}
}
